package com.example.warehouse.bus.service.impl;

import com.example.warehouse.bus.dao.persist.repository.IBusSalesRepository;
import com.example.warehouse.bus.dao.persist.repository.IBusSalesbackRepository;
import com.example.warehouse.bus.pojo.param.BusSalesUpdateParam;
import com.example.warehouse.bus.pojo.param.BusSalesbackSelectParam;
import com.example.warehouse.bus.pojo.param.BusSalesbackUpdateParam;
import com.example.warehouse.bus.pojo.vo.BusSalesbackSelectVO;
import com.example.warehouse.bus.service.IBusSalesbackService;
import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageBean;
import com.example.warehouse.common.security.CurrentPrincipal;
import com.example.warehouse.common.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 销售退货订单_业务层_实现类
 */
@Slf4j
@Service
public class BusSalesbackServiceImpl implements IBusSalesbackService {

    @Autowired
    private IBusSalesbackRepository iBusSalesbackRepository;
    @Autowired
    private IBusSalesRepository iBusSalesRepository;

    @Override
    public void addNew(Long id, String description, CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【新增_销售退货订单】的业务，参数：{},{},{}", id, description, currentPrincipal);

        Long idpipo = currentPrincipal.getId();
        int rows = iBusSalesbackRepository.insert(id, description, idpipo);

        BusSalesUpdateParam busSalesUpdateParam = new BusSalesUpdateParam();
        busSalesUpdateParam.setId(id);
        busSalesUpdateParam.setApprovalStatus(3);
        int rows2 = iBusSalesRepository.update(busSalesUpdateParam, idpipo);

        if (rows != 1) {
            String message = "新增_销售退货订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
        if (rows2 != 1) {
            String message = "新增_销售退货订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id, CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【逻辑删除_销售退货订单】的业务，参数：{},{}", id, currentPrincipal);
        Long idpipo = currentPrincipal.getId();
        int rows = iBusSalesbackRepository.delete(id, idpipo);
        if (rows != 1) {
            String message = "逻辑删除_销售退货订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void update(BusSalesbackUpdateParam busSalesbackUpdateParam, CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【任意项更改_销售退货订单】的业务，参数：{},{}", busSalesbackUpdateParam, currentPrincipal);
        Long idpipo = currentPrincipal.getId();
        int rows = iBusSalesbackRepository.update(busSalesbackUpdateParam, idpipo);
        if (rows != 1) {
            String message = "任意项更改_销售退货订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void approvalSalesback(Long id, Integer approval, CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【审批_销售退货订单】的业务，参数：{},{},{}", id, approval, currentPrincipal);
        Long idpipo = currentPrincipal.getId();
        int rows = iBusSalesbackRepository.approvalSalesback(id, approval, idpipo);
        if (rows != 1) {
            String message = "审批_销售退货订单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public PageBean page(BusSalesbackSelectParam busSalesbackSelectParam, Integer page, Integer pageSize) {
        log.debug("开始处理【分页模糊查询_销售退货订单】的业务，参数：{}", busSalesbackSelectParam);

        Integer ignore = (page - 1) * pageSize;
        List<BusSalesbackSelectVO> list = iBusSalesbackRepository.select(busSalesbackSelectParam, ignore, pageSize);

        Long count = iBusSalesbackRepository.count(busSalesbackSelectParam);

        PageBean pageBean = new PageBean(count, list);

        return pageBean;
    }
}
